import { Component, Prefab, ProgressBar, RichText, Scene, Sprite, _decorator, director } from "cc";
import GameRpc from "../api/GameRpc";
import { UIManager } from "./UIManager";
import { PrefabEnum } from "./PrefabEnum";
import ResManager from "./ResManager";


const { ccclass, property } = _decorator;

@ccclass('LoadCtrl')
export class LoadCtrl extends Component {

	@property({type: Sprite})
	public m_imgBG: Sprite;

	@property({type: ProgressBar})
	public m_mPB: ProgressBar;

	@property({type: RichText})
	public m_mRT: RichText;

	onLoad() {
		this.m_mPB.progress = 0;
		this.m_mRT.string = '0%';
		

	
		
	}

	loadScene(sceneName){
		ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.Loading)
		director.preloadScene(sceneName, (completedCount: number, totalCount: number, item: any) => {
			this.m_mPB.progress = completedCount / totalCount;
			this.m_mRT.string = `${Math.floor(completedCount * 100 / totalCount)}%`
		}, async (error: null | Error) => {
			await UIManager.instance.showLoading()
			await GameRpc.login()
			UIManager.instance.closeLoading()
			// UIManager.instance.tip('登录成功')
			director.loadScene(sceneName, (_, scene: Scene) => {});
		});
	}
}